<?php

/**
 * Class page
 * 显示样式
 *
 * 实例化分页显示 __construct($nowPage=1,$count=0,$pagecount=10){ //当前页默认1 总条数默认0 每页显示条数默认10条
 */
class page{

    private $page = 1; //当前页
    private $count; //总条数
    private $downpage; //每页显示条数
    private $pagecount; //总页数

    public function __construct($nowPage=1,$count=0,$downpage=10){ //当前页默认1 总条数默认0 每页显示条数默认10条

        $this->page = is_numeric($nowPage) ? $nowPage : 1 ; //当前页
        $this->count = is_numeric($count) ? $count : 0 ; //总条数
        $this->downpage = is_numeric($downpage) ? $downpage : 10 ; //每页显示条数
        $this->pagecount = ceil($this->count/$this->downpage);//获得总页数

        //当前页范围判断
        if($this->page>$this->pagecount){
            $this->page=$this->pagecount;
        }
        if($this->page<1){
            $this->page=1;
        }
    }

    //计算sql分页
    public function showlist(){ //当前页 显示条数
        $pageArray = array();
        //设定每页起始页
        $pageArray['startPage'] = $this->page*$this->downpage-$this->downpage;
        //设定显示长度
        $pageArray['startLen'] = $this->downpage;
        return $pageArray;
    }

    public function showpage(){
        //页码样式
        $str = "";
        $str.="当前{$this->page}/{$this->pagecount}";
        $str.=" <a i='1' href='index.php?page=1'>首页</a>";

        //上一页
        if($this->page==1){
            $str.=" <span>上一页</span>";
        }else{
            $str.=" <a href='index.php?page=".($this->page-1)."'>上一页</a>";
        }

        //页数小于12时显示全部
        if($this->pagecount<12){
            //总页数小于12页
            for($i=1;$i<=$this->pagecount;$i++){
                if($this->page==$i){
                    $str.="<b>$i</b>";
                }else{
                    $str .=" <a i='$i' href='index.php?page=$i'>$i</a> ";
                }
            }

        }elseif ($this->pagecount<=1){
            //总页数等于1时不显示
            return null;
        }else{
            //总页数大于等于12时显示部分
            //$len获取数据长度 $start数据起点
            $len = $this->page+3;
            $start = $this->page-3;

            //界线小于1时
            if($start <= 0){
                $start=1;
                $len=7;
            }

            if($this->pagecount-$this->page<9 ){
                $start=$this->pagecount-8;
                if($start <= 0){
                    $start=1;
                }
                $len=$this->pagecount;
            }

            if($this->page>=7){
                $str .=" <a i='1' href='index.php?page=1'>1</a> ";
                $str .=" <a i='2' href='index.php?page=2'>2</a> ";
                $str .="<b>...</b>";
            }else{
                $start=1;
                $len=$this->pagecount<=8?$this->pagecount:8;
            }

            for($i=$start;$i<=$len;$i++){
                if($this->page==$i){
                    $str.="<b>$i</b>";
                }else{
                    $str .=" <a i='$i' href='index.php?page=$i'>$i</a> ";
                }
            }

            if(($this->pagecount-$this->page)>=9 || $this->page<7){
                $str .="<b>...</b>";
                $str .=" <a i='".($this->pagecount-1)."' href='index.php?page=".($this->pagecount-1)."'>".($this->pagecount-1)."</a> ";
                $str .=" <a i='$this->pagecount' href='index.php?page=$this->pagecount'>$this->pagecount</a> ";
            }
        }

        //下一页
        if($this->page==$this->pagecount){
            $str.=" <span>下一页</span>";
        }else{
            $str.="<a href='index.php?page=".($this->page+1)."'>下一页</a> ";
        }

        $str.=" <a i='$this->pagecount' href='index.php?page=$this->pagecount'>末页</a>";
        echo $str;
    }

}

